package com.zhuoyue.biz.core.command.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.BaLiuXingRenTiGanYingMianBan0Faa8105CmdDeal;
import com.zhuoyue.biz.core.command.constant.LiuLuChangJing0Faa8A05CmdDeal;
import com.zhuoyue.common.utils.CmdUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * 四路联控面板
 *
 * @author wjming
 */
public class LiuLuChangjing0Faa8A05 implements Device {
    private static final String NIGHT_LIGHT_TIME = "nightLightTime";
    private static final String NIGHT_LIGHT_TIME_00 = "00";
    private static final String NIGHT_LIGHT_TIME_FF = "FF";
    private static final int NIGHT_LIGHT_TIME_10 = 10;
    private static final int NIGHT_LIGHT_TIME_60 = 60;

    @Override
    public String getCommand(String controlParam) {
        Map<String, String> map = (Map) JSONObject.parse(controlParam);
        String controlType = map.get("controlType");
        String cmd = "";

        if (StringUtils.isEmpty(controlType)) {
            Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
            if (StringUtils.isNotEmpty(actionsMap.get(NIGHT_LIGHT_TIME))) {
                cmd = getRadarontrolCmd(map);
                return cmd;
            }
            return null;
        }
        return cmd;
    }


    private String getRadarontrolCmd(Map<String, String> map) {

        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));

        String cmd10 = "";

        String nightLightTime = actionsMap.get("nightLightTime");
        if (NIGHT_LIGHT_TIME_FF.equals(nightLightTime) || NIGHT_LIGHT_TIME_00.equals(nightLightTime)) {
            cmd10 = nightLightTime;
            String cmd3To10 = LiuLuChangJing0Faa8A05CmdDeal.DirectControl.CMD_3 +

                    LiuLuChangJing0Faa8A05CmdDeal.DirectControl.CMD_4 +
                    map.get("gatewayMac") +
                    map.get("deviceMac") +

                    LiuLuChangJing0Faa8A05CmdDeal.DirectControl.RADAR +

                    LiuLuChangJing0Faa8A05CmdDeal.DirectControl.CHILEDOD +
                    LiuLuChangJing0Faa8A05CmdDeal.DirectControl.DATA +
                    cmd10;

            String cmd2 = CmdUtils.getDateLength(cmd3To10);

            String cmd11 = CmdUtils.getCmdBodySum(cmd3To10);

            String fullCmd = BaLiuXingRenTiGanYingMianBan0Faa8105CmdDeal.NightLightSet.CMD_1 +
                    cmd2 +
                    cmd3To10 +
                    cmd11 +
                    BaLiuXingRenTiGanYingMianBan0Faa8105CmdDeal.NightLightSet.CMD_12;
            return fullCmd;

        } else if (Integer.parseInt(nightLightTime) >= NIGHT_LIGHT_TIME_10 && Integer.parseInt(nightLightTime) <= NIGHT_LIGHT_TIME_60) {
            cmd10 = CmdUtils.intToHexTwo(Integer.parseInt(nightLightTime));
            String cmd3To10 = LiuLuChangJing0Faa8A05CmdDeal.DirectControl.CMD_3 +
                    LiuLuChangJing0Faa8A05CmdDeal.DirectControl.CMD_4 +
                    map.get("gatewayMac") +
                    map.get("deviceMac") +
                    LiuLuChangJing0Faa8A05CmdDeal.DirectControl.RADAR +
                    LiuLuChangJing0Faa8A05CmdDeal.DirectControl.CHILEDOD +
                    LiuLuChangJing0Faa8A05CmdDeal.DirectControl.DATA +
                    cmd10;

            String cmd2 = CmdUtils.getDateLength(cmd3To10);

            String cmd11 = CmdUtils.getCmdBodySum(cmd3To10);

            String fullCmd = BaLiuXingRenTiGanYingMianBan0Faa8105CmdDeal.NightLightSet.CMD_1 +
                    cmd2 +
                    cmd3To10 +
                    cmd11 +
                    BaLiuXingRenTiGanYingMianBan0Faa8105CmdDeal.NightLightSet.CMD_12;
            return fullCmd;
        } else {
            return null;
        }

    }

}
